<?php
if (! defined ( 'BASEPATH' ))
	exit ( 'No direct script access allowed' );

/**
 * 后台权限管理模型
 *
 * @category Models
 * @author 二　阳°(QQ:707069100)
 * @link http://weibo.com/513778937?topnav=1&wvr=5
 */
class Product_model extends Base_model {

    var $model_table='product';

	function __construct() {
        parent::__construct ();
	}

	// ------------------------------------------------------------------------
	
	/**
	 * 角色数据
     *
	 * @access public
	 * @return array 数据数组
	 */
	function product_datas($rows, $page) {
		if ($this->session->CI->_manager->role_id==1 || $this->session->CI->_manager->role_id==6) {
			$datas = $this->get_all_page ($this->model_table,$rows,$page );
		}else {
			$datas = $this->db->query("SELECT 
				    b.* FROM
				    cn_product b
				        inner JOIN
				    cn_product_user a on b.id = a.product_id and a.user_id=".$this->session->CI->_manager->id."
				     	;")->result_array ();	
		}

		// foreach ( $datas as $n => $data ) {
		// 	$datas [$n] ['use_count'] = $this->db->where ( 'role_id', $data ['id'] )->count_all_results ( 'manager' );
		// }
		return $datas;
	}
	
	function getProgress(){
		return $this->db->select ( 'id, name_cn as name' )->get (  $this->db->dbprefix ( "product_progress" ) )->result_array ();
	}

	function menu_datas($pid) {
		$this->load->helper ( 'my_tree' );
		isset ( $arg ['status'] ) ? $this->db->where ( 'status', $arg ['status'] ) : '';
		$tree =  $this->db->select ( '*' )->from ($this->db->dbprefix ( 'menu' ) )->order_by ( 'rank', 'asc' )->get ()->result_array () ;

		$files = $this->db->select ( 'mid,count(mid) as count' )->where ( 'pid =', $pid )->group_by('mid')->get (  $this->db->dbprefix ( "product_file" ) )->result_array ();

		$tree = classify_tree ($tree );
		foreach ($tree as &$value) {
			$value["count"] = 0;
		}

		foreach ($files as  $key => $v1) {
			$this->menuCount($v1["mid"],$v1["count"],$tree );
		}

		foreach ($tree as &$v2) {

			if ($v2["pid"]==0 || (isset($v2["have_child"]) && $v2["have_child"] )) {
				$v2["count"] = $this->menuDegui($v2,$tree);	
			}
		}

        return $tree; 
	}

	function menuDegui(&$node,$array){
		$result = 0;
		foreach ($array as $key => &$value) {
			if ( $node["id"]==$value["pid"] ) {
				if (isset($value["have_child"]) && $value["have_child"]) {
					$result += $this->menuDegui($value,$array);
				} else {
					$result += $value["count"];
				}
			}
		}
		return $result;
	}

	function menuCount($mid,$count,&$array){
		
		foreach ($array as $key => &$value) {
			if (isset($value["id"]) && $value["id"] == $mid) {
				$value["count"] = $count;
			}
		}
	}

	function file_datas($pid,$mid){
		$files = $this->db->select ( 'id, name,size ,mid' )->where ( 'pid =', $pid )->where('mid =',$mid)->order_by ( 'id', 'desc' )->get (  $this->db->dbprefix ( "product_file" ) )->result_array ();
		foreach ( $files as $n => &$data ) {
			if ($data["size"]) {
				$data["size"] = round($data["size"] / 1024 )."kb";
			}
		}
		return $files;
	}

	function user_datas($pid) {
		$users = $this->db->query("SELECT 
			    c.username,r.name,c.id,b.product_id is NULL as statu FROM
			    cn_product_user b
			        RIGHT JOIN
			    cn_manager c ON b.user_id = c.id and b.product_id = ".$pid."
					inner join
				cn_role r on r.id = c.role_id;")->result_array ();
        return $users;
	}

	function comment_datas($pid) {
		$users = $this->db->query("SELECT 
			    c.username,r.name,b.id,b.content FROM
			    cn_product_comment b
			        inner JOIN
			    cn_manager c ON b.userid = c.id and b.pid = ".$pid."
					inner join
				cn_role r on r.id = c.role_id;")->result_array ();
        return $users;
	}

	function adddefaultuser($pid) {
		$users = $this->db->query("SELECT 
			    c.id FROM
			    cn_manager c where role_id=6 or role_id=1;")->result_array ();
		foreach ($users as $key => $value) {
			$this->adduser($value["id"],$pid);
		}
        return $users;
	}


	function video_datas($pid) {
		$users = $this->db->query("SELECT 
			    c.username,r.name,b.id,b.name,b.imgpath FROM
			    cn_product_media b
			        inner JOIN
			    cn_manager c ON b.userid = c.id and b.pid = ".$pid."
					inner join
				cn_role r on r.id = c.role_id;")->result_array ();
        return $users;
	}

	function adduser($userid,$pid){
		$db_table = $this->db->dbprefix ( 'product_user' );
		$this->db->insert ( $db_table, array("user_id"=>$userid,"product_id"=>$pid) );
		return $this->db->insert_id ();
	}

	function deleteuser($userid,$pid){
		$db_table = $this->db->dbprefix ( 'product_user' );
		$this->db->delete ( $db_table, array("user_id"=>$userid,"product_id"=>$pid) );
		return $this->db->affected_rows ();
	}

	function deletevideo($userid){
		$db_table = $this->db->dbprefix ( 'product_media' );
		$this->db->delete ( $db_table, array("id"=>$userid) );
		return $this->db->affected_rows ();
	}

	function deletecomment($userid){
		$db_table = $this->db->dbprefix ( 'product_comment' );
		$this->db->delete ( $db_table, array("id"=>$userid) );
		return $this->db->affected_rows ();
	}


	function attach($value,$tree){
		foreach ($tree as $item) {
			
		}
	}

	function addfile($data){
		$db_table = $this->db->dbprefix ( 'product_file' );
		$this->db->insert ( $db_table, $data );
		return $this->db->insert_id ();
	}

	function deleteFile($data){
		$db_table = $this->db->dbprefix ( 'product_file' );
		$this->db->delete ( $db_table, $data );
		return $this->db->affected_rows ();
	}
	// ------------------------------------------------------------------------
}

/* End of file power_model.php */
/* Location: ./app/admin/models/power_model.php */
